1654D - Potion Brewing Class - CodeForces Solution


dfs and similar implementation math number theory trees *2100

Please click on ads to support us..

Python Code:

'''Author- Akshit Monga'''
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import deque,defaultdict
mod=998244353


def Sieve(limit=10 ** 6):
    
    isPrime = [1] * (limit + 1)
    isPrime[0] = isPrime[1] = 0
    for i in range(2, limit + 1):
        if isPrime[i] != 1: continue
        for j in range(i * i, limit + 1, i):
            isPrime[j] = i
    return isPrime

isPrime = Sieve(2 * 10 ** 5)


def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)

def lcm(a, b):
    return (a // gcd(a, b)) * b


from types import GeneratorType


def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc


@bootstrap
def dfs(x,p):
    global ans,vals,nums
    ans+=vals[x]
    ans%=mod
    for i,d1,c1 in g[x]:
        c,d=c1,d1
        if i==p:
            continue
        vals[i]=(vals[x]*c)%mod
        vals[i]=(vals[i]*pow(d,mod-2,mod))%mod
        if c >= 2:
            while isPrime[c] != 1:
                nums[isPrime[c]]+=1
                c //= isPrime[c]
            nums[c]+=1
        if d >= 2:
            while isPrime[d] != 1:
                nums[isPrime[d]]-=1
                lc[isPrime[d]]=max(lc[isPrime[d]],-nums[isPrime[d]])
                d //= isPrime[d]
            nums[d]-=1
            lc[d]=max(lc[d],-nums[d])
        yield dfs(i,x)
        c,d=c1,d1
        if c >= 2:
            while isPrime[c] != 1:
                nums[isPrime[c]] -= 1
                c //= isPrime[c]
            nums[c] -= 1
        if d >= 2:
            while isPrime[d] != 1:
                nums[isPrime[d]] += 1
                d //= isPrime[d]
            nums[d] += 1
    yield

t=int(input())
for _ in range(t):
    n=int(input())
    g=[[] for i in range(n)]
    nums=[0 for i in range(n+1)]
    vals=[1 for i in range(n+1)]
    lc = [0 for i in range(n+1)]
    ans=0
    for i in range(n-1):
        a,b,c,d=map(int,input().split())
        a-=1
        b-=1
        g[a].append((b,c,d))
        g[b].append((a,d,c))
    dfs(0,-1)
    for i in range(2,n+1):
        ans*=pow(i,lc[i],mod)
        ans%=mod
    print(ans)


Comments

Submit
0 Comments
More Questions

489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1